package com.naiterui.ehp.bp.utils.encypt;

import java.util.Random;

public class GengerCode {
	
	//短信验证码长度
	private static final int GEN_SMS_CODE_LENGTH = 4;
	//短信验证码种子
	private static final String BASE_SMS_CODES = "0123456789";
	
	//密码盐长度
	private static final int GEN_SALT_CODE_LENGTH = 10;
	//密码盐种子
	private static final String BASE_SALT_CODES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
	//医生邀请码长度
	private static final int GEN_DOC_CODE_LENGTH = 4;
	//医生邀请码种子
	private static final String BASE_DOC_CODES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
	/**
	 * 生成短信验证码
	 * @return
	 */
	public static String generateSmsCode(){
		return generateCode(GEN_SMS_CODE_LENGTH,BASE_SMS_CODES);
	}
	
	/**
	 * 生产密码盐
	 * @return
	 */
	public static String generateSaltCode(){
		return generateCode(GEN_SALT_CODE_LENGTH,BASE_SALT_CODES);
	}
	
	/**
	 * 生成邀请码，
	 * 建议生成规则为:前2位代表具体业务代表，后4位为具体邀请码
	 * 累计生成量最大为14776336(约1KW)
	 * @return
	 */
	public static String generateDocCode(){
		return generateCode(GEN_DOC_CODE_LENGTH,BASE_DOC_CODES);
	}
	
	public static String generateCode(int length,String code) {
		Random random = new Random(System.currentTimeMillis());
		StringBuilder randomCode = new StringBuilder();
		for (int i = 0; i < length; i++) {
			int start = random.nextInt(code.length());
			String strRand = code.substring(start, start + 1);
			randomCode.append(strRand);
		}
		return randomCode.toString();
	}
	
}
